//////////////////////////////////////////////////////////////////////////
// PeekPocket - the hassle-free WiFi scanner for Windows Mobile
// coded by dzolee
// http://dzolee.blogspot.com
//
// Setting storage: provides access to numeric/string settings that can
// be read from and written to registry.
//
//////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "SettingsStorage.h"

//////////////////////////////////////////////////////////////////////////
CSettingsStorage::CSettingsStorage()
{
	//initialization
	m_NumSet[nseFontSize] = NumericSetting(_T("FontSize"), true, 12, 0, false);
	m_NumSet[nseScanSpeed] = NumericSetting(_T("ScanSpeed"), true, 2, 0, false);
	m_NumSet[nseHideWEP] = NumericSetting(_T("HideWEP"), true, 0, 0, false);
	m_NumSet[nseHidePeer] = NumericSetting(_T("HidePeer"), true, 0, 0, false);
	m_NumSet[nseSoundOff] = NumericSetting(_T("SoundOff"), true, 1, 0, false);
	m_NumSet[nseLanguage] = NumericSetting(_T("Language"), true, leEnglish, 0, false);

	m_StrSet[sseAdapter] = StringSetting(_T("Adapter"), true, _T(""), _T(""), false);
}

//////////////////////////////////////////////////////////////////////////
CSettingsStorage::~CSettingsStorage()
{
}

//////////////////////////////////////////////////////////////////////////
//restore settings from registry
bool CSettingsStorage::Deserialize()
{
int i;
HKEY key;
DWORD dwCount;
bool bKeyOpened;

	bKeyOpened=false;
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\PeekPocket"), 0, 0, &key) == ERROR_SUCCESS)
	{
		bKeyOpened=true;
	}
	
	//if key open failed, initialize default values

	//load DWORD props
	for(i=0; i<nseMAX; i++)
	{
		dwCount=sizeof(DWORD);
		if(bKeyOpened == true && RegQueryValueEx(key, m_NumSet[i].m_strName, 0, 0, (LPBYTE)&m_NumSet[i].m_dwValue, &dwCount) == ERROR_SUCCESS)
		{
			//read successfull
			m_NumSet[i].m_bUsingDefault=false;
		}
		else
		{
			m_NumSet[i].m_dwValue=m_NumSet[i].m_dwDefault;
			m_NumSet[i].m_bUsingDefault=true;
		}
	}//for num props

	//load Str props
	for(i=0; i<sseMAX; i++)
	{
		dwCount=sizeof(TCHAR) * 64;
		if(bKeyOpened == true && RegQueryValueEx(key, m_StrSet[i].m_strName, 0, 0, (LPBYTE)m_StrSet[i].m_strValue.GetBuffer(64), &dwCount) == ERROR_SUCCESS)
		{
			//read successfull
			m_StrSet[i].m_bUsingDefault=false;
		}
		else
		{
			//read failed, use default
			m_StrSet[i].m_strValue=m_StrSet[i].m_strDefault;
			m_StrSet[i].m_bUsingDefault=true;
		}
	}//for str props
	
	if(bKeyOpened == true)
	{
		RegCloseKey(key);
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
//save settings to registry
bool CSettingsStorage::Serialize()
{
int i;
HKEY key;
DWORD dwDisp;

	if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\PeekPocket"), 0, 0, 0, 0, 0, &key, &dwDisp) == ERROR_SUCCESS)
	{
		//save DWORD props
		for(i=0; i<nseMAX; i++)
		{
			RegSetValueEx(key, m_NumSet[i].m_strName, 0, REG_DWORD, (BYTE *)&m_NumSet[i].m_dwValue, sizeof(DWORD));
		}

		//save Str props
		for(i=0; i<sseMAX; i++)
		{
			RegSetValueEx(key, m_StrSet[i].m_strName, 0, REG_SZ, (BYTE *)(LPCTSTR)m_StrSet[i].m_strValue, sizeof(TCHAR) * (m_StrSet[i].m_strValue.GetLength() + 1));	//.GetLength() does not return size of terminating zero
		}
		RegCloseKey(key);
	}
	else
	{
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
//return a numeric setting
bool CSettingsStorage::GetNumeric(NumericSettingsEnum nseItem, DWORD &dwOut)
{
	if(nseItem < nseMAX)
	{
		dwOut=m_NumSet[nseItem].m_dwValue;
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
//return a string setting
bool CSettingsStorage::GetString(StringSettingsEnum sseItem, CString &strOut)
{
	if(sseItem < sseMAX)
	{
		strOut=m_StrSet[sseItem].m_strValue;
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
//store a numeric value
bool CSettingsStorage::SetNumeric(NumericSettingsEnum nseItem, DWORD dwIn)
{
	if(nseItem < nseMAX)
	{
		m_NumSet[nseItem].m_dwValue=dwIn;
		m_NumSet[nseItem].m_bUsingDefault=false;
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
//store a string
bool CSettingsStorage::SetString(StringSettingsEnum sseItem, CString strIn)
{
	if(sseItem < sseMAX)
	{
		m_StrSet[sseItem].m_strValue=strIn;
		m_StrSet[sseItem].m_bUsingDefault=false;
		return true;
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
DWORD CSettingsStorage::GetScanSpeed()
{
DWORD dwScanSpeed;

	//get setting
	GetNumeric(nseScanSpeed, dwScanSpeed);
	switch(dwScanSpeed)
	{
		case 5:		//slowest
		{
			dwScanSpeed=5000;		//5 seconds
			break;
		}
		case 4:		//slow
		{
			dwScanSpeed=2500;
			break;
		}
		case 3:		//normal
		{
			dwScanSpeed=1500;
			break;
		}
		case 2:		//fast
		{
			dwScanSpeed=1000;
			break;
		}
		case 1:		//fastest
		{
			dwScanSpeed=500;
			break;
		}
		default:
			dwScanSpeed=REFRESH_RATE_DEFAULT;
	}
	return dwScanSpeed;
}
